﻿
using System;
using System.Web.UI;
using System.Data;

/// <summary>
/// This control used to get the Page number value and goto that page
/// </summary>
public partial class UserControls_GoToPage : System.Web.UI.UserControl
{
    public UserControls_GoToPage()
    {
        this.Load += new System.EventHandler(Page_Load);
    }
    # region Variable Declaration
    /// <summary>
    /// This variable used to hold the total
    /// </summary>
    private string totalRecords;

    //Dropdown Selected Index Changed
    public delegate void EventHandler(object sender, EventArgs e);
    public event EventHandler SelectedIndexChanged;

    //For Go button Click Event
    public event System.EventHandler GoClick;

    public static int PageSize = 25;
    # endregion

    # region Page Load
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {

            if (!Page.IsPostBack)
            {
                //Call this fucntion to load PageSize dropdown
                LoadPageSize();
                ddlPageSize.SelectedValue = Convert.ToString(PageSize);
                txtPageNumber.TabIndex = ddlPageSize.TabIndex;
                ibtPageGo.TabIndex = ddlPageSize.TabIndex;
            }
        }
        catch (Exception errorInGoToPage)
        {
            JSAlert.Show(errorInGoToPage.Message);
        }
    }

    # endregion

    # region User Procedures
    /// <summary>
    /// Loads the size of the page.
    /// </summary>
    private void LoadPageSize()
    {
        try
        {
            //Create instance of the DropDown class

            DataSet dsPageSize = new DataSet();
            DataTable dtValue = new DataTable();
            dtValue.Columns.Add("Value");
            dtValue.Columns.Add("Display");

            DataRow dr = dtValue.NewRow();

            for (int i = 1; i < 3; i++)
            {
                dr["Display"] = i == 1 ? "15 Enteries" : "25 Enteries";
                dr["Value"] = i == 1 ? "15" : "25";
                dtValue.Rows.Add(dr);
            }
            ddlPageSize.Items.Clear();
            ddlPageSize.DataSource = dtValue;
            // ddlPageSize.DataTextField = MFC.Business.Dropdown.DisplayField;
            // ddlPageSize.DataValueField = MFC.Business.Dropdown.ValueField;
            ddlPageSize.DataTextField = "Display";
            ddlPageSize.DataValueField = "Value";
            ddlPageSize.DataBind();

        }
        catch (Exception ex)
        {
            JSAlert.Show(ex.Message);
        }
    }

    /// <summary>
    /// Shows the size of the page.
    /// </summary>
    public void ShowPageSize()
    {
        try
        {
            ddlPageSize.SelectedValue = Convert.ToString(PageSize);
        }
        catch (Exception errorInGoToPage)
        {
            JSAlert.Show(errorInGoToPage.Message);
        }
    }

    /// <summary>
    /// Displays this instance.
    /// </summary>
    public void Display()
    {
        //lblCount.Text = totalRecords;
    }

    # endregion

    # region Property

    /// <summary>
    /// Gets or sets the total records.
    /// </summary>
    /// <value>The total records.</value>
    public string TotalRecords
    {
        set
        {
            lblCount.Text = value;
        }
        get
        {
            return lblCount.Text;
        }
    }

    /// <summary>
    /// Gets or sets the text.
    /// </summary>
    /// <value>The text.</value>
    public string Text
    {
        get
        {
            return txtPageNumber.Text;
        }
        set
        {
            txtPageNumber.Text = value;
        }
    }

    public short TabIndex
    {
        get
        {
            return ddlPageSize.TabIndex;
        }
        set
        {
            ddlPageSize.TabIndex = value;
        }
    }

    # endregion

    # region Events

    /// <summary>
    /// Handles the SelectedIndexChanged event of the ddlPageSize control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {

        try
        {
            PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);

            //Response.Redirect(Request.Url.AbsolutePath);  
            if (this.SelectedIndexChanged != null)
            {
                SelectedIndexChanged(this, new System.EventArgs());
            }
        }
        catch (Exception errorInGoToPage)
        {
            JSAlert.Show(errorInGoToPage.Message);
        }


    }

    /// <summary>
    /// Handles the Click event of the ibtPageGo control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
    protected void ibtPageGo_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (this.GoClick != null)
            {
                this.GoClick(sender, e);
            }
        }
        catch (Exception errorInGoToPage)
        {
            JSAlert.Show(errorInGoToPage.Message);
        }
    }
    # endregion
}
